package org.flint92.springboot.servlet;

import org.flint92.springboot.servlet.filter.MyFilterWithBean;
import org.flint92.springboot.servlet.servlet.MyServletWithBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;

import javax.servlet.Filter;
import javax.servlet.Servlet;

/**
 * @author flint92
 */
@SpringBootApplication
@ServletComponentScan(basePackages = "org.flint92.springboot.servlet")
public class SpringBootServletApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootServletApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        builder.sources(SpringBootServletApplication.class);
        return builder;
    }

    @Bean
    public ServletRegistrationBean<Servlet> servletRegistrationBean() {
        ServletRegistrationBean<Servlet> registrationBean = new ServletRegistrationBean<>();

        registrationBean.setServlet(new MyServletWithBean());
        registrationBean.addUrlMappings("/spring-api/servlet");

        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean<Filter> filterFilterRegistrationBean() {
        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();

        registrationBean.setFilter(new MyFilterWithBean());
        registrationBean.addUrlPatterns("/spring-api/servlet");

        return registrationBean;
    }

}
